﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.Modules.Version.AdminSource
{
    public partial class _ajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            //通过ccid传频道ID
            ChannelId = Str2Str(q("ccid"));
            id = Str2Str(q("id"));
            Admin_Load("", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxGetSimple":
                    ajaxGetSimple();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxGetList()
        {
            Admin_Load(ChannelId + "-04", "json", true);
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 20);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[UserId]=B.Id";
            string whereStr1 = "A.Id>0";//外围条件(带A.)
            string whereStr2 = "Id>0";//分页条件(不带A.)
            string keyWord = Jumbot.Common.Strings.SafetyStr(q("k"));
            if (keyWord.Length > 0)
            {
                whereStr1 += " AND A.Title2 LIKE '%" + keyWord + "%'";
                whereStr2 += " AND Title2 LIKE '%" + keyWord + "%'";
            }
            string state = q("state");
            switch (state)
            {
                case "0":
                    whereStr1 += " AND A.State=0";
                    whereStr2 += " AND State=0";
                    break;
                case "1":
                    whereStr1 += " AND A.State=1";
                    whereStr2 += " AND State=1";
                    break;
                case "2":
                    whereStr1 += " AND A.State=2";
                    whereStr2 += " AND State=2";
                    break;
                case "3":
                    whereStr1 += " AND A.State=3";
                    whereStr2 += " AND State=3";
                    break;
            }
            string sdate = q("d");
            if (sdate != "")
            {
                if (DBType == "0")
                {
                    switch (sdate)
                    {
                        case "1d":
                            whereStr1 += " AND datediff('d',A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff('d',adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1w":
                            whereStr1 += " AND datediff('ww',A.AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff('ww',AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1m":
                            whereStr1 += " AND datediff('m',A.AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff('m',AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1y":
                            whereStr1 += " AND A.AddDate>=#" + (DateTime.Now.Year + "-1-1") + "#";
                            whereStr2 += " AND AddDate>=#" + (DateTime.Now.Year + "-1-1") + "#";
                            break;
                    }
                }
                else
                {
                    switch (sdate)
                    {
                        case "1d":
                            whereStr1 += " AND datediff(d,A.adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff(d,adddate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1w":
                            whereStr1 += " AND datediff(ww,A.AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff(ww,AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1m":
                            whereStr1 += " AND datediff(m,A.AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            whereStr2 += " AND datediff(m,AddDate,'" + DateTime.Now.ToShortDateString() + "')=0";
                            break;
                        case "1y":
                            whereStr1 += " AND A.AddDate>='" + (DateTime.Now.Year + "-1-1") + "'";
                            whereStr2 += " AND AddDate>='" + (DateTime.Now.Year + "-1-1") + "'";
                            break;
                    }
                }
            }
            whereStr1 += " AND A.ChannelId=" + ChannelId;
            whereStr2 += " AND ChannelId=" + ChannelId;
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_module_version_source");
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.id as id,A.Title as Title,A.Title2 as Title2,B.Id as UserId,B.UserName as UserName,B.TrueName as TrueName,A.State as State,A.IsLocked as IsLocked,A.AddDate as AddDate,A.Author2 as Author2,A.WordCount2 as WordCount2,(SELECT Title FROM [jcms_module_version_source_sort] WHERE Id=a.SortId) as SortName", "jcms_module_version_source", "jcms_normal_user", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        private void ajaxGetSimple()
        {
            Admin_Load(ChannelId + "-04", "json", true);
            if (Str2Str(q("sourceid")) != "0")
            {
                doh.Reset();
                doh.SqlCmd = "SELECT id,Title,Title2,Author,Author2,AuthorSummary,AuthorSummary2,ArmsId,Tags,UserId,Summary,SourceFrom,SourceFrom2,SourceFromUrl2,Countries,WordCount,WordCount2,Translator,DownUrl1,DownUrl2 FROM [jcms_module_version_source] WHERE Id=" + Str2Str(q("sourceid")) + "and State=2";
                DataTable dt = doh.GetDataTable();
                if (dt.Rows.Count > 0)
                    this._response = "{result :\"1\"," +
                        "returnval :\"操作成功\"," +
                        Jumbot.Common.JsonHelp.DtToSON(dt) +
                        "}";
                else
                    this._response = JsonResult(0, "无记录");
                dt.Clear();
                dt.Dispose();
            }
            else
                this._response = JsonResult(0, "参数有误");
        }
        private void ajaxDel()
        {
            Admin_Load(ChannelId + "-04", "json", true);
            string sId = f("id");
            doh.Reset();
            doh.ConditionExpress = "id=" + sId + " and state=0";
            if (doh.Delete("jcms_module_version_source") != 0)
                this._response = JsonResult(1, "成功删除");
            else
                this._response = JsonResult(0, "删除有误");
        }
    }
}